<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Lab5 FAQ</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<h1> Lab5 FAQ</h1>
<p> 
	This document contains Frequently Asked Questions,  which are relevant to completing the week's lab
assignment.  Any Questions may be posted to <a href="mailto:cspp51081@cs.uchicago.edu">cspp51081</a>.	
</p>
<h3>Where to begin.</h3>
<p> Get started on this assignment early.  First, finish the
Grand Ole Shell.  There are two parts of this project:<br>
<ul>
	<li>Parsing the input line</li>
	<li>Creating processes to run commands using <font face="Courier New, Courier, mono">fork</font>
	 and <font face="Courier New, Courier, mono">exec</font>.</li>
</ul>
You will probably find the first part is the harder of the two, because it can lead to some rather
nasty C programming.  (Watch out for those <font face="Courier New, Courier, mono">SIGSEGV</font>s.
Plan to give yourself some time to debug.  I will give you some hints below that will take a long
way on this part of the project.  The second part is significantly easier to program, but the more
difficult and important conceptually.  You need to establish a strong foundation in process control, 
the rest of this course depends upon it.  I will give you some pointers below to get started, and
some places to look for more information.
</p>
<h3>String Processing</h3>
<p>
     Get acquainted with C's standard string library, any C book will have a thorough discussion of it.
A string is of type <font face="Courier New, Courier, mono">char *	</font>, and ALWAYS ends in the
null character <font face="Courier New, Courier, mono">'\0'</font>.  Here are some useful functions:
all require <font face="Courier New, Courier, mono">#include &lt;string.h&gt;</font><br>
	 
<ul>
	<li><font face="Courier New, Courier, mono">char *strtok( char *str, const char *set );</font><br>
		This function is used to separate a string <font face="Courier New, Courier, mono">str</font>
		into tokens (or fields) by using the characters in <font face="Courier New, Courier, mono">set</font>
		 as separators.   Here is how it works:  The first time you call <font face="Courier New, Courier, mono">strtok</font>
		 to separate <font face="Courier New, Courier, mono">str</font>, you call<br>
		 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font face="Courier New, Courier, mono">strtok( str, set );</font><br>
		<font face="Courier New, Courier, mono">strtok</font> will skip over any members of 
		<font face="Courier New, Courier, mono">set</font> until it comes to the first character
		 not in <font face="Courier New, Courier, mono">set</font>; it will place a pointer at this position
		of <font face="Courier New, Courier, mono">str</font>.  It will then look for the next character
		in <font face="Courier New, Courier, mono">set</font>, and write a null character <font face="Courier New, Courier, mono">'\0'</font> in place of this character.
		That is the end of your first string token.  Your are returned a pointer to it.  Note well:  Your string
		has been overwritten.  Every subsequent call to <font face="Courier New, Courier, mono">strtok</font>, to obtain all other tokens must pass 
		<font face="Courier New, Courier, mono">NULL</font> in place of <font face="Courier New, Courier, mono">str</font>:<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font face="Courier New, Courier, mono">strtok( NULL, set );</font><br>
		and <font face="Courier New, Courier, mono">strtok</font> will return a pointer to the next string token
		 in your string <font face="Courier New, Courier, mono">str</font>.  <font face="Courier New, Courier, mono">strtok</font> will return
		 <font face="Courier New, Courier, mono">NULL</font> when no more tokens are found.  You may change
		 the argument <font face="Courier New, Courier, mono">set</font> on subsequent calls to <font face="Courier New, Courier, mono">strtok</font>, but you must pass <font face="Courier New, Courier, mono">NULL</font> if you want
		 <font face="Courier New, Courier, mono"> strtok</font> to continue parsing <font face="Courier New, Courier, mono">str</font>.
		<br></li>
		
		<li><font face="Courier New, Courier, mono">int strcmp( const char *s1, const char *s2);</font><br>
		     <font face="Courier New, Courier, mono">int strncmp( const char *s1, const char *s2, size_t n);</font><br>
			 <font face="Courier New, Courier, mono">strcmp</font> compares the two strings <font face="Courier New, Courier, mono">s1</font>
			  and <font face="Courier New, Courier, mono">s2</font> by <em>dictionary</em> order, and returns
			  an integer value less than zero if <font face="Courier New, Courier, mono">s1</font> comes before <font face="Courier New, Courier, mono">s2</font>,
			   equal to zero if they are the same string,
			  and greater than zero if  comes after <font face="Courier New, Courier, mono">s2</font>.
			  <font face="Courier New, Courier, mono">strncmp</font> compares on no more than the first n characters of each string.
			 </li>
</ul>
</p>
<h3>Process Control</h3>
<p>  There are three good sources for information about process control from your texts:
	<ul>
	<li>  Molay, <em>Understanding Unix/Linux Programming</em>, Chapter 8, is an excellent
		discussion which is very relevant to this project.</li>
		<li>Stevens, <em>Advanced Programming for the Unix Environment</em>, Chapter 8, will
			give you a thorough background in Processes.</li>
		<li>Stone, <em>Basic Linux Programming</em>, Chapter 10, a good introduction, but
			not sufficient by itself for a solid understanding.</li>	

	</ul>
In addition to these references, if will look for a good and reasonably short tutorial to help; you 
also have your manpages, section 2.  The following functions are very important:<br>
<ul>
	<li><font face="Courier New, Courier, mono">fork</font></li>
	<li><font face="Courier New, Courier, mono">exec</font> family of six functions (all of them
		use <font face="Courier New, Courier, mono">execve</font>, although you will find <font face="Courier New, Courier, mono">execvp</font>
		 and <font face="Courier New, Courier, mono">execlp</font> most useful)</li>
	<li><font face="Courier New, Courier, mono">wait</font></li>
	<li><font face="Courier New, Courier, mono">exit</font> (You probably know all
	  about this one,	but it is in the same important family of functions as
	  the other three.)<br></li>
	  	 </ul>	
Remember, it will pay you well if you spend some time learning to use these functions, and 
understanding how Unix manages processes by means of them.  
</p>
</body>
</html>
